package com.coolw.cola.statemachine.config;

import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import com.coolw.cola.statemachine.dto.Order;
import com.coolw.cola.statemachine.enums.OrderEvent;
import com.coolw.cola.statemachine.enums.OrderStatusEnum;
import com.coolw.cola.statemachine.support.CloseOrderCondition;
import com.coolw.cola.statemachine.support.CloseOrderAction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 状态机配置
 *
 * @author coolw
 * @date 2024/4/9 14:03
 */
@Slf4j
@Configuration
public class OrderStateMachineConfig {

    @Resource
    private CloseOrderAction closeOrderAction;

    @Resource
    private CloseOrderCondition closeOrderCondition;

    @Bean("orderStateMachine")
    public StateMachine<OrderStatusEnum, OrderEvent, Order> orderStateMachine() {
        String machineId = "ORDER_STATE_MACHINE";

        StateMachineBuilder<OrderStatusEnum, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
        // 待付款 | 待发货->订单关闭，用户关闭事件
        builder.externalTransitions()
                .fromAmong(OrderStatusEnum.WAIT_PAYMENT, OrderStatusEnum.WAIT_DELIVERY)
                .to(OrderStatusEnum.CLOSE)
                .on(OrderEvent.USER_CLOSE)
                .when(closeOrderCondition)
                .perform(closeOrderAction);
        // 待付款 | 已发货 | 待发货->订单关闭，后台操作关闭事件
        builder.externalTransitions()
                .fromAmong(OrderStatusEnum.WAIT_PAYMENT, OrderStatusEnum.HAVE_BEEN_DELIVERY, OrderStatusEnum.WAIT_DELIVERY)
                .to(OrderStatusEnum.CLOSE)
                .on(OrderEvent.ADMIN_CLOSE)
                .when(closeOrderCondition)
                .perform(doAction());
        // 待发货->订单关闭，超时关闭事件
        builder.externalTransition()
                .from(OrderStatusEnum.WAIT_DELIVERY)
                .to(OrderStatusEnum.CLOSE)
                .on(OrderEvent.OVERTIME_CLOSE)
                .when(closeOrderCondition)
                .perform(doAction());
        // 待发货->订单关闭，审批不通过事件
        builder.externalTransition()
                .from(OrderStatusEnum.WAIT_DELIVERY)
                .to(OrderStatusEnum.CLOSE)
                .on(OrderEvent.AUDIT_CLOSE)
                .when(closeOrderCondition)
                .perform(doAction());
        // 待付款->待发货,用户支付完成事件
        builder.externalTransition()
                .from(OrderStatusEnum.WAIT_PAYMENT)
                .to(OrderStatusEnum.WAIT_DELIVERY)
                .on(OrderEvent.USER_PAY)
                .when(closeOrderCondition)
                .perform(doAction());

        // 构建状态机
        StateMachine<OrderStatusEnum, OrderEvent, Order> orderStateMachine = builder.build(machineId);

        // UML图
        String plantUML = orderStateMachine.generatePlantUML();
        log.info("orderStateMachine----plantUML:{}", plantUML);

        return orderStateMachine;
    }

    private Action<OrderStatusEnum, OrderEvent, Order> doAction() {
        return (from, to, event, ctx) ->
                log.info("[{}]订单正在操作,orderId:{},from:{},to:{},on:{}", ctx.getMessage(), ctx.getOrderId(), from, to, event);
    }
}
